//
//  LGLungFollowEditCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/5.
//  肺癌-随访计划-编辑

#import "LGLungFollowEditTaskCell.h"
#import "LGFollowKeywordView.h"
#import "LGInputAlertView.h"
@interface LGLungFollowEditTaskCell()<LGFollowKeywordDelegate>
/**   */
@property (nonatomic , strong)UIView *backView;
/**   */
@property (nonatomic , strong)UIView *projectView;
@property (nonatomic , strong)UILabel *projectLabel;
/**   */
@property (nonatomic , strong)UIView *evaluateView;
@property (nonatomic , strong)UILabel *evaluateLabel;
/**   */
@property (nonatomic , strong)UIView *publicView;
@property (nonatomic , strong)UILabel *publicLabel;

/**   */
@property (nonatomic , strong)UIView *keywordView;
@property (nonatomic , strong)UIButton *keywordBtn;

/**   */
@property (nonatomic , strong)UIButton *deleteBtn;
@property (nonatomic , strong)UIView *maskView;
/**   */
@property (nonatomic , strong)UIButton *addButton;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGLungFollowEditTaskCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.type = LGPositionTypeMiddle;
        [self createSubView];
    }
    return  self;
}

-(void)setCandelete:(BOOL)candelete{
    _candelete = candelete;
    self.maskView.hidden = candelete;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    
    
    NSArray *examsArray = dataDic[@"exams"];
    NSString *project = @"";
    for (NSDictionary *tempDic in examsArray) {
        NSArray *tempArray = tempDic[@"codes"][@"names"];
        for (NSString *content in tempArray) {
            project = (project.length>0)?[NSString stringWithFormat:@"%@、%@",project,content]:content;
        }
    }
    self.projectLabel.text = (project.length>0)?project:@" ";
    
    NSArray *evaArray = dataDic[@"evas"];
    NSString *evaStr = @"";
    for (NSDictionary *tempDic in evaArray) {
        NSArray *tempArray = tempDic[@"codes"][@"names"];
        for (NSString *content in tempArray) {
            evaStr = (evaStr.length>0)?[NSString stringWithFormat:@"%@、%@",evaStr,content]:content;
        }
    }
    self.evaluateLabel.text = (evaStr.length>0)?evaStr:@" ";
    
    NSArray *preachArray = dataDic[@"preachs"];
    NSString *preachStr = @"";
    for (NSDictionary *tempDic in preachArray) {
        NSArray *tempArray = tempDic[@"codes"][@"names"];
        for (NSString *content in tempArray) {
            preachStr = (preachStr.length>0)?[NSString stringWithFormat:@"%@、%@",preachStr,content]:content;
        }
    }
    self.publicLabel.text = (preachStr.length>0)?preachStr:@" ";
    
    
    
    //关键字暂时去掉 -- 目前没有做处理
//    for (LGFollowKeywordView *itemView in self.keywordView.subviews) {
//        [itemView removeFromSuperview];
//    }
//    self.itemArray = [NSMutableArray array];
//    NSArray *keywordsArray = dataDic[@"keywords"];
//    CGFloat itemTop = 0;
//    CGFloat itemLeft = 0;
//    CGFloat itemBottom = 0;
//    CGFloat maxWidth = Screen_W-viewPix(56);
//    for (NSInteger i=0 ; i<keywordsArray.count; i++) {
//        NSString *content = LGNSString(keywordsArray[i]);
//        CGFloat contentW = [content textWidthWithFont:LGFont(15) maxHeight:MAXFLOAT]+viewPix(40);
//        contentW = (contentW>maxWidth)?maxWidth:contentW;
//        CGFloat contentH = [content textHeightWithFont:LGFont(15) maxWidth:maxWidth-viewPix(40)];
//        itemTop = (itemLeft+contentW)>maxWidth?(itemTop+contentH+viewPix(28)):itemTop;
//        itemLeft = (itemLeft+contentW)>maxWidth?0:itemLeft;
//        LGFollowKeywordView *itemView = [[LGFollowKeywordView alloc]initWithFrame:CGRectMake(itemLeft, itemTop, contentW, contentH+viewPix(15))];
//        itemView.content = content;
//        [self.keywordView addSubview:itemView];
//        itemLeft += contentW+viewPix(8);
//        itemView.delegate = self;
//        itemView.tag = 1000+i;
//        [self.itemArray addObject:itemView];
//        itemBottom = kMaxY(itemView.frame);
//    }
//    [self.keywordView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(@(itemBottom));
//    }];
}

//删除
-(void)deleteButtonAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(deleteTaskAtIndexPath:)]) {
        [self.delegate deleteTaskAtIndexPath:self.indexPath];
    }
}

//新增
-(void)addButtonAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addTaskAtIndexPath:)]) {
        [self.delegate addTaskAtIndexPath:self.indexPath];
    }
}

//添加关键字
-(void)addKeywordAction{
    LGInputAlertView *nameView = [[LGInputAlertView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    nameView.placeHolder = @"请输入关键字";
    nameView.title = @"添加关键字";
    __weak typeof(self) weakSelf = self;
    nameView.groupNameFinished = ^(NSString * _Nonnull content) {
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:weakSelf.dataDic];
        NSMutableArray *examsArray = [NSMutableArray arrayWithArray:tempDic[@"exams"]];
        NSMutableDictionary *examDic = [NSMutableDictionary dictionaryWithDictionary:@{@"codes":@[],@"keywords":@[]}];
        if (examsArray.count>0) {
            examDic = [NSMutableDictionary dictionaryWithDictionary:examsArray[0]];
        }
        NSMutableArray *keywordsArray = [NSMutableArray arrayWithArray:examDic[@"keywords"]];
        [keywordsArray addObject:content];
        [examDic setValue:keywordsArray forKey:@"keywords"];
        [examsArray replaceObjectAtIndex:0 withObject:examDic];
        [tempDic setValue:examDic forKey:@"exams"];
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(editKeywords:indexPath:)]) {
            [weakSelf.delegate editKeywords:tempDic indexPath:weakSelf.indexPath];
        }
    };
    [[ObjectTool mainWindow] addSubview:nameView];
}

//删除关键字
-(void)deleteKeywordItem:(NSInteger)index{
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.dataDic];
    NSMutableArray *examsArray = [NSMutableArray arrayWithArray:tempDic[@"exams"]];
    NSMutableDictionary *examDic = [NSMutableDictionary dictionaryWithDictionary:@{@"codes":@[],@"keywords":@[]}];
    if (examsArray.count>0) {
        examDic = [NSMutableDictionary dictionaryWithDictionary:examsArray[0]];
    }
    NSMutableArray *keywordsArray = [NSMutableArray arrayWithArray:examDic[@"keywords"]];
    if (keywordsArray.count>index) {
        [keywordsArray removeObjectAtIndex:index];
    }
    [examDic setValue:keywordsArray forKey:@"keywords"];
    [examsArray replaceObjectAtIndex:0 withObject:examDic];
    [tempDic setValue:examDic forKey:@"exams"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(editKeywords:indexPath:)]) {
        [self.delegate editKeywords:tempDic indexPath:self.indexPath];
    }
}

//随访项目、评估量表、宣教项目
-(void)tapGestureAction:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.backView];
    if (self.delegate && [self.delegate respondsToSelector:@selector(editTaskAction:title:dataDic:key:indexPath:)]) {
        if (CGRectContainsPoint(self.projectView.frame, point)) {
            //选择随访项目
            [self.delegate editTaskAction:@"/common/dict/examsDict" title:@"添加随访项目" dataDic:self.dataDic key:@"exams" indexPath:self.indexPath];
            
        }else if (CGRectContainsPoint(self.evaluateView.frame, point)){
            //选择评估量表
            [self.delegate editTaskAction:@"/doctor/select/scalesSelect" title:@"添加评估量表" dataDic:self.dataDic key:@"evas" indexPath:self.indexPath];
            
        }else if (CGRectContainsPoint(self.publicView.frame, point)){
            //选择宣教项目
            [self.delegate editTaskAction:@"/doctor/select/preachSelect" title:@"添加宣教项目" dataDic:self.dataDic key:@"preachs" indexPath:self.indexPath];
        }
    }
}

-(void)createSubView{
    [self.backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)]];
    [self.baseView addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(8));
        make.left.equalTo(self.contentView).offset(viewPix(28));
        make.right.equalTo(self.contentView).offset(-viewPix(28));
        make.bottom.equalTo(self.contentView).offset(-viewPix(8));
    }];
    
    //随访项目
    UILabel *projectTitle = [UILabel lableWithText:@"随访项目" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    [self.backView addSubview:projectTitle];
    [self.backView addSubview:self.projectView];
    [self.projectView addSubview:self.projectLabel];
    [projectTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(viewPix(12));
        make.left.equalTo(self.backView).offset(viewPix(10));
        make.right.equalTo(self.backView).offset(-viewPix(10));
    }];
    [self.projectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(projectTitle.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(projectTitle);
    }];
    [self.projectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.projectView).offset(viewPix(10));
        make.left.equalTo(self.projectView).offset(viewPix(12));
        make.right.equalTo(self.projectView).offset(-viewPix(28));
        make.bottom.equalTo(self.projectView).offset(-viewPix(10));
    }];
   
    //评估量表
    UILabel *evaluateTitle = [UILabel lableWithText:@"评估量表" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    [self.backView addSubview:evaluateTitle];
    [self.backView addSubview:self.evaluateView];
    [self.evaluateView addSubview:self.evaluateLabel];
    
    [evaluateTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.projectView.mas_bottom).offset(viewPix(16));
        make.left.right.equalTo(projectTitle);
    }];
    [self.evaluateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(evaluateTitle.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(projectTitle);
    }];
    [self.evaluateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.evaluateView).offset(viewPix(10));
        make.left.equalTo(self.evaluateView).offset(viewPix(12));
        make.right.equalTo(self.evaluateView).offset(-viewPix(28));
        make.bottom.equalTo(self.evaluateView).offset(-viewPix(10));
    }];
   
    //宣教项目
    UILabel *publicTitle = [UILabel lableWithText:@"宣教项目" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    [self.backView addSubview:publicTitle];
    [self.backView addSubview:self.publicView];
    [self.publicView addSubview:self.publicLabel];
    [publicTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.evaluateView.mas_bottom).offset(viewPix(16));
        make.left.right.equalTo(projectTitle);
    }];
    [self.publicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(publicTitle.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(projectTitle);
        make.bottom.equalTo(self.backView).offset(-viewPix(10));
    }];
    [self.publicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.publicView).offset(viewPix(10));
        make.left.equalTo(self.publicView).offset(viewPix(12));
        make.right.equalTo(self.publicView).offset(-viewPix(28));
        make.bottom.equalTo(self.publicView).offset(-viewPix(10));
    }];
    
    
//    UILabel *keywordTitle = [UILabel lableWithText:@"关键字" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
//    [self.backView addSubview:keywordTitle];
//    [self.backView addSubview:self.keywordBtn];
//    [self.backView addSubview:self.keywordView];
//    [keywordTitle mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.publicView.mas_bottom).offset(viewPix(16));
//        make.left.right.equalTo(projectTitle);
//    }];
//    [self.keywordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.backView).offset(-viewPix(10));
//        make.centerY.equalTo(keywordTitle);
//        make.width.height.equalTo(@(viewPix(35)));
//    }];
//    [self.keywordView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(keywordTitle.mas_bottom).offset(viewPix(8));
//        make.left.right.equalTo(projectTitle);
//        make.height.equalTo(@(0));
//        make.bottom.equalTo(self.backView).offset(-viewPix(10));
//    }];
    
//    [self.backView addSubview:self.deleteBtn];
//    [self.backView addSubview:self.addButton];
//    [self.backView addSubview:self.maskView];
//    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.keywordView.mas_bottom).offset(viewPix(16));
//        make.left.equalTo(self.backView).offset(viewPix(10));
//        make.right.equalTo(self.backView.mas_centerX).offset(-viewPix(8));
//        make.bottom.equalTo(self.backView).offset(-viewPix(10));
//        make.height.equalTo(@(viewPix(32)));
//    }];
//    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.deleteBtn.mas_right).offset(viewPix(16));
//        make.centerY.width.height.equalTo(self.deleteBtn);
//    }];
//    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.keywordView.mas_bottom).offset(viewPix(16));
//        make.left.equalTo(self.backView).offset(viewPix(10));
//        make.right.equalTo(self.backView.mas_centerX).offset(-viewPix(8));
//        make.height.equalTo(@(viewPix(32)));
//    }];
}

-(UIView *)backView{
    if(!_backView){
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = [UIColor colorWithString:@"#FAFAFA"];
        _backView.cornerRidus = 10;
    }
    return _backView;
}

-(UIView *)projectView{
    if(!_projectView){
        _projectView = [[UIView alloc]init];
        _projectView.backgroundColor = [UIColor whiteColor];
        [_projectView addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        [_projectView addSubview:tipView];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_projectView).offset(-viewPix(12));
            make.centerY.equalTo(_projectView);
            make.width.height.equalTo(@(16));
        }];
    }
    return _projectView;
}

-(UILabel *)projectLabel{
    if(!_projectLabel){
        _projectLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _projectLabel;
}

-(UIView *)evaluateView{
    if(!_evaluateView){
        _evaluateView = [[UIView alloc]init];
        _evaluateView.backgroundColor = [UIColor whiteColor];
        [_evaluateView addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        [_evaluateView addSubview:tipView];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_evaluateView).offset(-viewPix(12));
            make.centerY.equalTo(_evaluateView);
            make.width.height.equalTo(@(16));
        }];
    }
    return _evaluateView;
}

-(UILabel *)evaluateLabel{
    if(!_evaluateLabel){
        _evaluateLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _evaluateLabel;
}

-(UIView *)publicView{
    if(!_publicView){
        _publicView = [[UIView alloc]init];
        _publicView.backgroundColor = [UIColor whiteColor];
        [_publicView addBorder:@"#EEEEEE" lineWidth:1 cornerRidus:3];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        [_publicView addSubview:tipView];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_publicView).offset(-viewPix(12));
            make.centerY.equalTo(_publicView);
            make.width.height.equalTo(@(16));
        }];
    }
    return _publicView;
}

-(UILabel *)publicLabel{
    if(!_publicLabel){
        _publicLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _publicLabel;
}

-(UIView *)keywordView{
    if(!_keywordView){
        _keywordView = [[UIView alloc]init];
    }
    return _keywordView;
}

-(UIButton *)keywordBtn{
    if(!_keywordBtn){
        _keywordBtn = [UIButton buttonWithImage:@"分组管理-编辑" selectImage:@"分组管理-编辑"];
        [_keywordBtn addTarget:self action:@selector(addKeywordAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _keywordBtn;
}

-(UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithTitle:@"删除" titleFont:LGFont(17) normalColor:@"#FF3B30" selectColor:@"#FF3B30"];
        [_deleteBtn addBorder:@"#FF3B30" lineWidth:1 cornerRidus:viewPix(16)];
        [_deleteBtn addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithTitle:@"新增" titleFont:LGFont(17) normalColor:@"#5E8EF5" selectColor:@"#5E8EF5"];
        [_addButton addBorder:@"#5E8EF5" lineWidth:1 cornerRidus:viewPix(16)];
        [_addButton addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

-(UIView *)maskView{
    if(!_maskView){
        _maskView = [[UIView alloc]init];
        _maskView.backgroundColor = [UIColor colorWithString:@"#FFFFFF" alpha:0.7];
        _maskView.hidden = self.candelete;
    }
    return _maskView;
}
@end
